﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;
using DVK.SP2007;
using Microsoft.SharePoint.Utilities;
using DVK.SP2007.UniversalClient;
using DVK.SP2007.Constants;
using System.Diagnostics;

namespace DVK.SP2007.Pages
{
    /// <summary>
    /// Page to update item metadata so that it would be processes again by Send/Receive process.
    /// </summary>
    public partial class ProcessAgain : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Get the item url
            string itemUrl = Request.Params[Constants.Params.QueryStringParameterItemUrl];

            SPWeb web = SPContext.Current.Web;

            web.AllowUnsafeUpdates = true;

            try
            {
                SPListItem item = web.GetListItem(itemUrl);
                //Update the item so, that it would be processed again
                item[Fields.ToBeProcessed.Name] = true;
                item[Fields.StatusUpdateNeeded.Name] = true;
                item[Fields.SendingStatus.Name] = (int)DvkSendingStatus.WaitingInDHS;
                item[Fields.SendingStatusText.Name] =
                    new AspNetResourceManager().GetResourceBasedOnEnumElement<DvkSendingStatus>(DvkSendingStatus.WaitingInDHS);

                item.UpdateOverwriteVersion();
            }
            catch (Exception exc)
            {
                //Failed to update the item
                throw new DvkException(
                        string.Format("ProcessAgain.aspx: Failed to update the item. Exception details: {0}", exc.ToString())
                    );
            }

            web.AllowUnsafeUpdates = false;

            SPUtility.Redirect(itemUrl.Substring(0,itemUrl.LastIndexOf('/')), SPRedirectFlags.Default, HttpContext.Current);
        }
    }
}
